package org.jeecg.modules.wlhy.home;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressCommon;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressCommonService;
import org.jeecg.modules.wlhy.home.service.HyHomeService;
import org.jeecg.modules.wlhy.home.vo.ShippingMonthTotalVO;
import org.jeecg.modules.wlhy.order.entity.OrderMonthTotalDTO;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.entity.ShippingMonthTotalDTO;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.tj.mapper.HyTjMapper;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;


@Api(tags="首页")
@RestController
@RequestMapping("/hyhome")
@Slf4j
public class HyHomeController extends JeecgController<HyCustomerAddressCommon, IHyCustomerAddressCommonService> {

    @Resource
    private HyTjMapper hyTjMapper;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private IHyOrderService hyOrderService;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private HyHomeService hyHomeService;

    /**
     * 工作台快速开始模块
     * 运输数据、运单对账、运单支付、数据上报、申请开票、司机管理、车辆管理、用户管理
     * @param mode
     * @return
     */
    @GetMapping(value = "/getQuickStart")
    public Result getQuickStart(@RequestParam(name = "mode", required = false) Integer mode) {
        return this.hyHomeService.getQuickStartData(mode);
    }


    @GetMapping(value = "/query")
    public Result<JSONObject> queryById(@RequestParam(name = "t") String t) {
        String dateString = DateUtils.getDate("yyyy-MM-dd");
        JSONObject jo = new JSONObject();

        if("1".equals(t)){

        }
        String customerId = "all";
        HyCustomer customer = userUtil.getCustomer();
        if(customer != null){
            customerId =customer.getId();
        }

        Map<String,String> orderData = hyTjMapper.getHomeOrderTj(customerId);
        jo.put("order", orderData);
        Map<String,String> shippingData = hyTjMapper.getHomeShippingTj(customerId);
        jo.put("shipping", shippingData);

        Map<String,String> payData = hyTjMapper.getHomePayTj(customerId);
        jo.put("pay", payData);

        return Result.OK(jo);
    }

    @GetMapping(value = "/getMonthTotal")
    public Result<?> getMonthTotal() {
        String[]  latestMonth = DateUtils.getLatest12Month().split("=");
        String customerId = "all";
        HyCustomer customer = userUtil.getCustomer();
        if(customer != null){
            customerId =customer.getId();
        }
        List<ShippingMonthTotalDTO> shippingMonthTotalDTOS = hyShippingNoteService.getShippingMonthTotal(customerId , latestMonth[0] , latestMonth[1]);
        return Result.OK(shippingMonthTotalDTOS);
    }

    @GetMapping(value = "/getShippingAndOrderMonthTotal")
    public Result<?> getShippingAndOrderMonthTotal() {
        String[]  latestMonth = DateUtils.getLatest12Month().split("=");
        String customerId = "all";
        HyCustomer customer = userUtil.getCustomer();

        if(customer != null){
            customerId =customer.getId();
        }
        List<ShippingMonthTotalDTO> shippingMonthTotalDTOS = hyShippingNoteService.getShippingMonthTotal(customerId , latestMonth[0] , latestMonth[1]);
        List<OrderMonthTotalDTO>  orderMonthTotalDTOS = hyOrderService.getOrderMonthTotal(customerId ,latestMonth[0] , latestMonth[1]);

        Map<String, Integer> orderMonthTotalDTOMap = new HashMap <>();
        if(CollectionUtils.isNotEmpty(orderMonthTotalDTOS)){
            for (OrderMonthTotalDTO orderMonthTotalDTO : orderMonthTotalDTOS) {
                orderMonthTotalDTOMap.put(orderMonthTotalDTO.getMonths(), orderMonthTotalDTO.getOrderTotal());
            }
        }

        List<ShippingMonthTotalVO> shippingMonthTotalVOs = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(shippingMonthTotalDTOS)){
            for(ShippingMonthTotalDTO shippingMonthTotalDTO :shippingMonthTotalDTOS){

                ShippingMonthTotalVO shippingMonthTotalVO = new ShippingMonthTotalVO();
                shippingMonthTotalVO.setType(shippingMonthTotalDTO.getMonths()+"月");
                shippingMonthTotalVO.setMonths(shippingMonthTotalDTO.getMonths());
                shippingMonthTotalVO.setShippingTotal(shippingMonthTotalDTO.getShippingTotal());
                shippingMonthTotalVO.setOrderTotal(
                         MapUtils.isNotEmpty(orderMonthTotalDTOMap) &&
                                 orderMonthTotalDTOMap.get(shippingMonthTotalDTO.getMonths()) != null ?
                                 orderMonthTotalDTOMap.get(shippingMonthTotalDTO.getMonths())  : 0
                );
                shippingMonthTotalVOs.add(shippingMonthTotalVO) ;
            }
        }
//        LOGGER.info("getShippingAndOrderMonthTotal | {} ",shippingMonthTotalVOs);

        /*//测试折线图
        ShippingMonthTotalVO shippingMonthTotalVOs1 = new ShippingMonthTotalVO();
        shippingMonthTotalVOs1.setType("1月");
        shippingMonthTotalVOs1.setMonths("1");
        shippingMonthTotalVOs1.setShippingTotal(44);
        shippingMonthTotalVOs1.setOrderTotal(10);

        ShippingMonthTotalVO shippingMonthTotalVOs2 = new ShippingMonthTotalVO();
        shippingMonthTotalVOs2.setType("2月");
        shippingMonthTotalVOs2.setMonths("2");
        shippingMonthTotalVOs2.setShippingTotal(100);
        shippingMonthTotalVOs2.setOrderTotal(12);

        List<ShippingMonthTotalVO> testShippingMonthTotalVO = new ArrayList<>();
        testShippingMonthTotalVO.add(shippingMonthTotalVOs1);
        testShippingMonthTotalVO.add(shippingMonthTotalVOs2);*/


        return Result.OK(shippingMonthTotalVOs);
    }


}
